#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <algorithm>
#include <limits>
#include <time.h>
using namespace std;

#define Max 10000
#define RAND_MAX 100

struct Point
{
	float x;
	float y;	
};
void RandomGeneratePoints(Point p[], int n);

int random_in_range (unsigned int min, unsigned int max)
{
	//Randomize
	int base_random = rand(); /* in [0, RAND_MAX] */
	if (RAND_MAX == base_random) return random_in_range(min, max);
	/* now guaranteed to be in [0, RAND_MAX) */
	int range       = max - min,
    remainder   = RAND_MAX % range,
    bucket      = RAND_MAX / range;
	/* There are range buckets, plus one smaller interval
     within remainder of RAND_MAX */
	if (base_random < RAND_MAX - remainder) {
		return min + base_random/bucket;
	} else {
		return random_in_range (min, max);
	}
}

void RandomGeneratePoints(Point p[], int n)
{
	for(int i= 0; i<n; i++)
	{
		p[i].x = random_in_range(2,100);
		p[i].y = random_in_range(1,100);
	}
}

void PrintPoints(Point p[], int n)
{
	for(int i= 0; i<n; i++)
	{
		printf("P%d(%2f,%2f)\n", i, p[i].x, p[i].y);
	}
}
void BruteForceClosestPoint(Point p[], int n)
{
	float distance = 1000000000;
	int p1, p2;
	for(int i = 1; i <= n-1; i++)
	{
		for(int j = i+1; j <= n; j++)
		{
			float d = sqrt( (p[i].x - p[j].x)*(p[i].x - p[j].x) + (p[i].y - p[j].y)*(p[i].y - p[j].y) );

			if (d < distance)
			{
				distance = d;
				p1 = i;
				p2 = j;
				
			}
		}
	}
	printf("Diem 1: (%0.2f,%0.2f) | Diem 2: (%0.2f, %0.2f)\n", p[p1].x, p[p1].y, p[p2].x, p[p2].y);
	printf("Khoang cach: %f", distance);
	//return distance;	
}

void main()
{
	Point p[Max];
	int n = 1000;
	RandomGeneratePoints(p,n);
	//PrintPoints(p,n);
	BruteForceClosestPoint(p,n);

	getch();
}
